package com.ulic.customerManApplication.tree;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.ulic.customerManApplication.base.BaseAction;


public abstract class BaseTreeAjax  extends BaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected static final String contextPath = ServletActionContext.getRequest().getContextPath();
	protected static final String TRUE = "TRUE";
	protected static final String FALSE = "FALSE";
	protected static final String VLINE_IMAGE = "/html/image/treevertline.gif";
	protected static final String BLANK_IMAGE = "/html/image/treeblank.gif";
	protected static final String LAST_LEAF_IMAGE = "/html/image/treelastnode.gif";
	protected static final String LEAF_IMAGE = "/html/image/treenode.gif";
	protected static final String OPEN_BRANCH_IMGAGE = "/html/image/treemnode.gif";
	protected static final String CLOSE_BRANCH_IMGAGE = "/html/image/treepnode.gif";
	protected static final String LAST_OPEN_BRANCH_IMGAGE = "/html/image/treemlastnode.gif";
	protected static final String LAST_CLOSE_BRANCH_IMGAGE = "/html/image/treeplastnode.gif";

	public abstract Tree getTree();
	public abstract String getNameLink(TreeNode tn);
	public abstract String getNameLinkTarget();

	public String getLink(TreeNode tn) {
		return "javascript:callTreeService('" + tn.getCode() + "')";
	}

	public String getTreeHTML(String code) throws Exception {
		StringBuffer buffer = new StringBuffer();
		Tree tree = getTree();
		
		TreeNode tn = tree.getNode(code);
		tn.setOpen(!tn.isOpen());
		
		TreeNode rootNode = tree.getRootNode();
		assembleRoot(buffer, rootNode);
		
        List<TreeNode> childNodes = rootNode.getChildNodes();
        
        for (int i = 0, size = childNodes.size(); i < size; i++) {
            render(tree, buffer, new ArrayList<String>(), childNodes.get(i), i == size - 1 ? true : false);
        }
		return buffer.toString();
	}
	
	public String clickNode(String code) throws Exception {
		StringBuffer buffer = new StringBuffer();
		Tree tree = getTree();
		
		tree.clearSelectedState();
		TreeNode tn = tree.getNode(code);
		if(tn != null)
			tn.setSelected(true);
		
		TreeNode rootNode = tree.getRootNode();
		assembleRoot(buffer, rootNode);
		
        List<TreeNode> childNodes = rootNode.getChildNodes();
        
        for (int i = 0, size = childNodes.size(); i < size; i++) {
            render(tree, buffer, new ArrayList<String>(), childNodes.get(i), i == size - 1 ? true : false);
        }
		return buffer.toString();
	}

	protected void assembleRoot(StringBuffer sb, TreeNode treeNode) {
		sb.append("<table cellspacing=\"0\" cellpadding=\"4\" border=\"0\">");
		if(treeNode.isSelected()){
			sb.append("<td nowrap=\"nowrap\" id=\"1\" style=\"background-color:#00469c;\">");
		} else {
			sb.append("<td nowrap=\"nowrap\" id=\"1\">");
		}
		// name link
		String nameLink = getNameLink(treeNode);
		if(treeNode.isHasLink()) {
			sb.append("<a href=\"");
			if (nameLink.startsWith("javascript")) {
				sb.append("#\" onclick=\"");
			} else {
				sb.append(contextPath);
			}
			sb.append(nameLink);
			sb.append("\" style=\"");
			sb.append("text-decoration:none;");
			if(treeNode.isSelected()){
				sb.append("color:#fff;font-weight:bold;");
			}
			sb.append("\" onclick=\"treeCurrentNode('1');\"");
			String nameLinkTarget = getNameLinkTarget();
			if (nameLinkTarget != null) {
				sb.append(" target=\"");
				sb.append(nameLinkTarget);
				sb.append("\"");
			}
			sb.append(" >");
		}// has link

		sb.append(treeNode.getName());
		
		if(treeNode.isHasLink()) {
			sb.append("</a>");
		}//has link
		
		sb.append("</td>");
		sb.append("</table>");
	}

	protected void render(Tree sourceTree, StringBuffer buffer, List<String> flags,
			TreeNode treeNode, boolean last) throws Exception {
		if (treeNode == null)
			return;
		buffer
				.append("<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">");
		for (int i = 0, size = flags.size(); i < size; i++) {
			if (((String) flags.get(i)).equalsIgnoreCase(TRUE)) {
				buffer.append("<td nowrap=\"nowrap\"><img src=\"").append(
						contextPath).append(VLINE_IMAGE).append("\"/></td>");
			} else if (((String) flags.get(i)).equalsIgnoreCase(FALSE)) {
				buffer.append("<td nowrap=\"nowrap\"><img src=\"").append(
						contextPath).append(BLANK_IMAGE).append("\"/></td>");
			}
		}

		if (!treeNode.hasChildren()) {
			buffer.append("<td nowrap=\"nowrap\"><img src=\"").append(
					contextPath);
			if (last) {
				buffer.append(LAST_LEAF_IMAGE);
			} else {
				buffer.append(LEAF_IMAGE);
			}
			buffer.append("\" border=\"0\"/></td>");
		} else {
			buffer.append("<td nowrap=\"nowrap\">");
			buffer.append("<a href=\"");
			buffer.append(getLink(treeNode));
			buffer.append("\"");
			buffer.append(" >");
			// image
			buffer.append("<img src=\"").append(contextPath);
			if (last) {
				if (treeNode.isOpen()) {
					buffer.append(LAST_OPEN_BRANCH_IMGAGE);
				} else {
					buffer.append(LAST_CLOSE_BRANCH_IMGAGE);
				}
			} else {
				if (treeNode.isOpen()) {
					buffer.append(OPEN_BRANCH_IMGAGE);
				} else {
					buffer.append(CLOSE_BRANCH_IMGAGE);
				}
			}
			buffer.append("\" border=\"0\"/>");
			buffer.append("</a>");
			buffer.append("</td>");
		}
		if(treeNode.isSelected()){
			buffer.append("<td nowrap=\"nowrap\" style=\"background-color:#00469c;padding:0 1px;\" id=\"" + treeNode.getCode() + "\">");
		} else {
			buffer.append("<td nowrap=\"nowrap\" style=\"padding:0 1px;\" id=\"" + treeNode.getCode() + "\">");
		}
		String nameLink = getNameLink(treeNode);
		if(treeNode.isHasLink()) {
			buffer.append("<a href=\"");
			if (nameLink.startsWith("javascript")) {
				buffer.append("#\" target=\"_self\"  onclick=\"");
			} else {
				buffer.append(contextPath);
			}
			buffer.append(nameLink);
			buffer.append("\" style=\"text-decoration:none;");
			if(treeNode.isSelected()){
				buffer.append("color:#fff;font-weight:bold;");
			}
			buffer.append("\"");
			String nameLinkTarget = getNameLinkTarget();
			buffer.append(" target=\"");
			buffer.append(nameLinkTarget);
			buffer.append("\"");
			buffer.append(" onClick=\"treeCurrentNode('" + treeNode.getCode() + "');\"");
			buffer.append(" >");
		}// has link

		buffer.append(treeNode.getName());
		if(treeNode.isHasLink()) {
			buffer.append("</a>");
		}// has link
		buffer.append("</td>");
		// current node end
		buffer.append("</table>");
		List<TreeNode> childNodes = treeNode.getChildNodes();
		if (treeNode.isOpen()
				&& childNodes != null && childNodes.size() > 0) {
			if (last) {
				flags.add(FALSE);
			} else {
				flags.add(TRUE);
			}
			for (int i = 0, size = childNodes.size(); i < size; i++) {
				render(sourceTree, buffer, flags, (TreeNode) childNodes.get(i),
						i == size - 1 ? true : false);
			}
		}
		if (last) {
			if (flags.size() > 0) {
				flags.remove(flags.size() - 1);
			}
		}

	}
}
